package com.benjaminwan.read.free.utils;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HiPDAPassUtils {
    public static String processedPassword(String pass) {
        if (TextUtils.isEmpty(pass) || pass.length() == 32)
            return pass;
        try {
            return md5(pass.replace("\\", "\\\\")
                    .replace("'", "\'")
                    .replace("\"", "\\\""));
        } catch (Exception e) {
            return pass;
        }
    }

    public static String md5(String content) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        byte[] hash;
        hash = MessageDigest.getInstance("MD5").digest(content.getBytes("UTF-8"));

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
}
